/**
 * step 2 预算出价
 */
define(['../../common/service', './createAdUtils'],
    function (http, adUtils) {
        var Step2 = {
            init: function () {
                var _this = this;
                _this.schedule_init([{id:'0',text:I18N.getText("createAd","continuously")},
                    {id:'1',text:I18N.getText("createAd","cycle")}],
                    I18N.getText("createAd","continuously"),0);
                _this.optimizing_init();
                _this.charge_init([
                    {id:'LINK_CLICKS',name:I18N.getText("createAd","linkClicks")},
                    {id:'IMPRESSIONS',name:I18N.getText("createAd","impressions")}],I18N.getText("createAd","linkClicks"),'LINK_CLICKS');
                _this.budget_init();
                _this.active_init();
                _this._datedrop_init(0);
                _this.price_init();
            },
            price_init : function(){
                $('input[name="price"]').off("click").on("click", function(e){
                    var v = $(this).val();
                    if (v == 'true'){
                        $("div.budget-show-div").addClass("hidden");
                    } else {
                        $("div.budget-show-div").removeClass("hidden");
                        $(".price-input").val("");
                    }
                });
            }
            , schedule_init: function (sheduleData,val,dataVal) {
                var _this = this;
                $("div.ndp-drop-wrapper[name=schedule-drop]").empty().drop({
                    caret:"glyphicon-menu-right",
                    data: sheduleData,
                    name: "schedule",//为了方便serialize 建议设置name 属性
                }).off("ITEM_CLICK").on("ITEM_CLICK", function (e) {//下拉选择项点击事件
                    _this._datedrop_init(e.originalEvent.data.val);
                }).val(val);
                $("input[name=schedule]").attr('data-val',dataVal);
            }
            , optimizing_init: function () {
                var _this = this;
                $("div.ndp-drop-wrapper[name=optimizing-drop]").empty().drop({
                    caret:"glyphicon-menu-right",
                    data: [ {id:'LINK_CLICKS',text:I18N.getText("createAd","linkClicks")},
                        {id:'IMPRESSIONS',text:I18N.getText("createAd","impressions")},
                        {id:'OFFSITE_CONVERSIONS',text: I18N.getText("createAd","offsiteConversions")}],
                    name: "optimizing"//为了方便serialize 建议设置name 属性
                }).off("ITEM_CLICK").on("ITEM_CLICK", function (e) {//下拉选择项点击事件
                    if(e.originalEvent.data.val == "OFFSITE_CONVERSIONS"){
                        //显示活动类型
                        $("#budget .activeType-wrapper").css("visibility","visible");
                        _this.charge_init([
                            {id:'IMPRESSIONS',name:I18N.getText("createAd","impressions")}],I18N.getText("createAd","impressions"),'IMPRESSIONS');
                    }else if(e.originalEvent.data.val == "IMPRESSIONS"){
                        $("#budget .activeType-wrapper").css("visibility","hidden");
                        _this.charge_init([
                            {id:'IMPRESSIONS',name: I18N.getText("createAd","impressions")}], I18N.getText("createAd","impressions"),'IMPRESSIONS');
                    }else{
                        $("#budget .activeType-wrapper").css("visibility","hidden");
                        _this.charge_init([
                            {id:'LINK_CLICKS',name:I18N.getText("createAd","linkClicks")},
                            {id:'IMPRESSIONS',name:I18N.getText("createAd","impressions")}],I18N.getText("createAd","linkClicks"),'LINK_CLICKS');
                    }
                }).val(I18N.getText("createAd","linkClicks"));
                $("input[name=optimizing]").attr('data-val','LINK_CLICKS');
            }
            , active_init: function () {
                var _this = this;
                $("div.ndp-drop-wrapper[name=activeType-drop]").empty().drop({
                    caret:"glyphicon-menu-right",
                    data: [
                        {id:'PURCHASE',text:I18N.getText("createAd","purchase")},
                        {id:'ADD_TO_CART',text:I18N.getText("createAd","addToCart")}],
                    name: "activeType"//为了方便serialize 建议设置name 属性
                }).off("ITEM_CLICK").on("ITEM_CLICK", function (e) {//下拉选择项点击事件
                }).val(I18N.getText("createAd","purchase"));
                $("input[name=activeType]").attr('data-val','PURCHASE');
            }
            , charge_init: function (chargeData,val,dataVal) {
                var _this = this;
                $("div.ndp-drop-wrapper[name=charge-drop]").empty().drop({
                    caret:"glyphicon-menu-right",
                    data: chargeData,
                    name: "charge"//为了方便serialize 建议设置name 属性
                }).off("ITEM_CLICK").on("ITEM_CLICK", function (e) {//下拉选择项点击事件
                }).val(val);
                $("input[name=charge]").attr('data-val',dataVal);
            }
            , budget_init: function () {
                var _this = this;
                $("div.ndp-drop-wrapper[name=budget-drop]").empty().drop({
                    caret:"glyphicon-menu-right",
                    data: [ {id:'0',text:I18N.getText("createAd","dailyBudget")},
                        {id:'1',text:I18N.getText("createAd","lifetimeBudget")}],
                    name: "budget"//为了方便serialize 建议设置name 属性
                }).off("ITEM_CLICK").on("ITEM_CLICK", function (e) {//下拉选择项点击事件
                    // set schedule input : must 周期
                    if(e.originalEvent.data.val == "0"){
                        //显示活动类型

                        _this.schedule_init([{id:'0',text:I18N.getText("createAd","continuously")},
                                {id:'1',text:I18N.getText("createAd","cycle")}],
                            I18N.getText("createAd","continuously"),0);


                        _this.schedule_init([{id:'0',text:I18N.getText("createAd","continuously")}, {id:'1',text:I18N.getText("createAd","cycle")}],I18N.getText("createAd","continuously"),0);
                        _this._datedrop_init(0);
                    }else{
                        _this.schedule_init([{id:'0',text:I18N.getText("createAd","continuously"),disable:true}, {id:'1',text:I18N.getText("createAd","cycle")}],I18N.getText("createAd","cycle"),1);
                        _this._datedrop_init(1);
                    }
                }).val(I18N.getText("createAd","dailyBudget"));
                $("input[name=budget]").attr('data-val',0);
            }
            , _datedrop_init: function (flag) {
                if(flag == 0){
                    //显示持续
                    $("#budget .schedule-goon").show();
                    $("#budget .daterangepicker-sample3").css("width","200px");
                    $('.daterangepicker-sample3').daterangepicker({
                            "opens": "nxdc-single",
                            timePicker: true,
                            autoApply: false,
                            timePicker12Hour:false,
                            timePicker24Hour:true,
                            timePickerSeconds:false,
                            timePickerIncrement:1,
                            //timezone:adUtils.userTimeZone,
                            //format: 'YYYY-MM-DD HH:mm',
                            singleDatePicker : true,
                            locale: {
                                format: 'YYYY-MM-DD HH:mm:ss'
                            },
                            minDate:moment().utcOffset(window.sessionStorage.timezoneValue).format('YYYY-MM-DD HH:mm:ss'),  //不能小于当前时间
                            startDate:moment().utcOffset(window.sessionStorage.timezoneValue).format('YYYY-MM-DD' +
                                ' HH:mm:ss'),
                            drops:'down'
                        },
                        function (start, end, label) {
                        });
                }else{
                    //隐藏持续
                    $("#budget .schedule-goon").hide();

                    $("#budget .daterangepicker-sample3").css("width","320px");
                    $('.daterangepicker-sample3').daterangepicker({
                        "opens": "nxdc",
                        "alwaysShowCalendars": true,
                        timePicker: true,
                        autoApply: true,
                        timePicker12Hour:false,
                        timePicker24Hour:true,
                        timePickerSeconds:false,
                        timePickerIncrement:1,
                        locale: {
                            format: 'YYYY-MM-DD HH:mm:ss'
                        },
                        startDate:moment().utcOffset(window.sessionStorage.timezoneValue).format('YYYY-MM-DD HH:mm:ss'),
                        endDate:moment().subtract(-1, 'day').utcOffset(window.sessionStorage.timezoneValue).format('YYYY-MM-DD' +
                            ' HH:mm:ss'),
                        drops:'down'
                    }, function(start, end, label) {
                    });
                }
            }
        }
        return Step2;
    });
